#include "playscene.h"

PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
{

}

PlayScene::PlayScene(int level)
{
    DataConfig dataConfig;

    qDebug()<< "its level "<<level;
    this->levelIndex = level;

    this->setFixedSize(320, 588);
    this->setWindowTitle("play");
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));
    // 创建菜单
    QMenuBar *bar = menuBar();
    this->setMenuBar(bar);
    QMenu *menu = bar->addMenu("begin");
    QAction *quitMenu = menu->addAction("quit");

    // 准备音乐
    QSound* backSound = new QSound(":/BackButtonSound.wav");
    QSound* coinSound = new QSound(":/ConFlipSound.wav");
    QSound* winSound = new QSound(":/LevelWinSound.wav");

    connect(quitMenu, &QAction::triggered, this, [=](){
        this->close();
    });
    // 返回按钮
    MyPushButton* backButton = new MyPushButton(":/BackButton.png", ":/BackButtonSelected.png");
    backButton->move(this->width() - backButton->width() - 10, this->height() - backButton->height() - 10);
    backButton->setParent(this);
    backButton->show();

    connect(backButton, &QPushButton::clicked, this, [=](){
        qDebug()<<"play back..back..";
        backSound->play();
        emit this->quitPlay();
    });

    // 显示具体关卡数
    QLabel* level_label = new QLabel;
    level_label->setParent(this);
    QString level_str = QString("leave:%1").arg(this->levelIndex);
    level_label->setText(level_str);
    // 设置显示字体
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    level_label->setFont(font);
    //
    level_label->setGeometry(QRect(30, this->height()-50, 120, 50));// 设置大小位置

    // 设置获胜标题
    successLabel = new QLabel;
    QPixmap pixmap;
    pixmap.load(":/LevelCompletedDialogBg.png");
    successLabel->setFixedSize(pixmap.width(), pixmap.height());
    successLabel->setPixmap(pixmap);
    successLabel->move((this->width() - pixmap.width()) / 2 ,-pixmap.height());
    successLabel->setParent(this);


    // 通过dataconfig数据
    QVector<QVector<int>> iconData = dataConfig.mData[this->levelIndex];
    // 将游戏地图数据设置到gameData中
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            gameArray[i][j] = iconData[i][j];
        }
    }



    // 创建背景图
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
           QLabel *bg = new QLabel(this);
           QPixmap pixmap;
           pixmap.load(":/BoardNode.png");
           bg->setGeometry(0, 0, pixmap.width(), pixmap.height());
           bg->setPixmap(pixmap);
           bg->move(57 + i*50, 200+j*50);
           // 绘制
           QString coinPath;
           if(gameArray[i][j] == 1){
               // 绘制金币
               coinPath = QString(":/Coin000%1.png").arg(1);
           }else if(gameArray[i][j] == 0){
               // 绘制银币
               coinPath = QString(":/Coin000%1.png").arg(8);
           }
           MyCoin* coin = new MyCoin(coinPath, gameArray[i][j]);
           coin->setParent(this);
           coin->move(59 + i*50, 204+j*50);
           coin->m_x = i;
           coin->m_y = j;
           // 将金币保存到金币数组中
           coinArr[i][j] = coin;

           // 设置金币点击效果
           connect(coin, &QPushButton::clicked, this, [=](){
               // 翻硬币
               coin->changeStatus();
               // 改变状态
               gameArray[coin->m_x][coin->m_y] = gameArray[coin->m_x][coin->m_y] == 0 ? 1 : 0;
               // 翻转其他银币

               // 解决手速问题
               // 先设置为true 点击完成后 如果没有胜利 就变成false
               for(int i = 0; i < 4; i++){
                   for(int j = 0; j < 4; j++){
                       coinArr[i][j]->btnWin = true;
                   }
               }
               // coinSound
               coinSound->play();

               // 延迟反转
               QTimer::singleShot(200, this, [=](){
                   // 翻转右侧银币
                   if(coin->m_x + 1 <= 3){
                       coinArr[coin->m_x + 1][coin->m_y]->changeStatus();
                       gameArray[coin->m_x+1][coin->m_y] = gameArray[coin->m_x+1][coin->m_y] == 0 ? 1 : 0;
                   }
                   // 翻转左侧银币
                   if(coin->m_x - 1 >= 0){
                       coinArr[coin->m_x - 1][coin->m_y]->changeStatus();
                       gameArray[coin->m_x-1][coin->m_y] = gameArray[coin->m_x-1][coin->m_y] == 0 ? 1 : 0;
                   }
                   // 翻转下侧银币
                   if(coin->m_y + 1 <= 3){
                       coinArr[coin->m_x][coin->m_y + 1]->changeStatus();
                       gameArray[coin->m_x][coin->m_y + 1] = gameArray[coin->m_x][coin->m_y + 1] == 0 ? 1 : 0;
                   }
                   // 反转上侧银币
                   if(coin->m_y - 1 >= 0){
                       coinArr[coin->m_x][coin->m_y - 1]->changeStatus();
                       gameArray[coin->m_x][coin->m_y - 1] = gameArray[coin->m_x][coin->m_y - 1] == 0 ? 1 : 0;
                   }

                   // 判断是否coinArr中的每一个flag 都是1
                   this->gameWin = true;
                   for(int i = 0; i < 4; i++){
                       for(int j = 0; j < 4; j++){
                           if(coinArr[i][j]->flag == 0){
                               this->gameWin = false;
                               break;
                           }
                       }
                   }

                   // 设置回来
                   for(int i = 0; i < 4; i++){
                       for(int j = 0; j < 4; j++){
                           coinArr[i][j]->btnWin = false;
                       }
                   }

                   if(this->gameWin){
                       qDebug()<<"you win !!!!";
                       // 设置 使得按钮按下没有效果
                       for(int i = 0; i < 4; i++){
                           for(int j = 0; j < 4; j++){
                               coinArr[i][j]->btnWin = true;
                           }
                       }
                       // 播放音乐
                       winSound->play();
                       // 天降胜利
                       QPropertyAnimation* animation = new QPropertyAnimation(this->successLabel, "geometry");
                       animation->setDuration(1000);
                       animation->setStartValue(QRect(successLabel->x(), successLabel->y(), successLabel->width(), successLabel->height()));
                       animation->setEndValue(QRect(successLabel->x(), successLabel->y() + 190, successLabel->width(), successLabel->height()));
                       animation->setEasingCurve(QEasingCurve::OutBounce);
                       animation->start();
                   }

               });

           });
        }
    }



}

void PlayScene::paintEvent(QPaintEvent* event){
    QPainter painter(this);
    QPixmap pixmap;
    pixmap.load(":/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0, this->width(), this->height(), pixmap);
}
